package com.peng.immodule.chat;

import com.peng.immodule.adapter.IMAConversation;
import com.peng.immodule.adapter.IMBase;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Mr.Q on 2018/3/6.
 * 描述
 */

public class IMConversation extends IMBase<IMAConversation> {
    private static final String TAG = "conversation";
    private static final int LIST_SIZE = 512;

    IMConversation(IMAConversation var1) {
        this.emaObject = var1;
    }

    public String conversationId() {
        return ((IMAConversation) this.emaObject).conversationId();
    }

    public IMConversation.IMConversationType getType() {
        IMAConversationType var1 = ((IMAConversation) this.emaObject)._getType();
        return var1 == IMAConversationType.CHAT ? IMConversation.IMConversationType.Chat : (var1 == IMAConversationType.GROUPCHAT ? IMConversation.IMConversationType.GroupChat : (var1 == IMAConversationType.CHATROOM ? IMConversation.IMConversationType.ChatRoom : (var1 == IMAConversationType.DISCUSSIONGROUP ? IMConversation.IMConversationType.DiscussionGroup : (var1 == IMAConversationType.HELPDESK ? IMConversation.IMConversationType.HelpDesk : IMConversation.IMConversationType.Chat))));
    }

    public int getUnreadMsgCount() {
        return ((IMAConversation) this.emaObject).unreadMessagesCount();
    }

    public void markAllMessagesAsRead() {
        ((IMAConversation) this.emaObject).markAllMessagesAsRead(true);
    }

    public int getAllMsgCount() {
        return ((IMAConversation) this.emaObject).messagesCount();
    }

    public List<EMMessage> loadMoreMsgFromDB(String var1, int var2) {
        List var3 = ((IMAConversation) this.emaObject).loadMoreMessages(var1, var2, EMASearchDirection.UP);
        ArrayList var4 = new ArrayList();
        Iterator var5 = var3.iterator();

        while (var5.hasNext()) {
            EMAMessage var6 = (EMAMessage) var5.next();
            if (var6 != null) {
                var4.add(new EMMessage(var6));
            }
        }

        this.getCache().addMessages(var4);
        return var4;
    }

    public List<EMMessage> searchMsgFromDB(long var1, int var3, IMConversation.EMSearchDirection var4) {
        EMASearchDirection var5 = var4 == IMConversation.EMSearchDirection.UP ? EMASearchDirection.UP : EMASearchDirection.DOWN;
        List var6 = ((IMAConversation) this.emaObject).searchMessages(var1, var3, var5);
        Object var7;
        if (var6.size() > 512) {
            var7 = new LinkedList();
        } else {
            var7 = new ArrayList();
        }

        Iterator var8 = var6.iterator();

        while (var8.hasNext()) {
            EMAMessage var9 = (EMAMessage) var8.next();
            if (var9 != null) {
                ((List) var7).add(new EMMessage(var9));
            }
        }

        return (List) var7;
    }

    public List<EMMessage> searchMsgFromDB(Type var1, long var2, int var4, String var5, IMConversation.EMSearchDirection var6) {
        EMASearchDirection var7 = var6 == IMConversation.EMSearchDirection.UP ? EMASearchDirection.UP : EMASearchDirection.DOWN;
        List var8 = ((IMAConversation) this.emaObject).searchMessages(var1.ordinal(), var2, var4, var5, var7);
        Object var9;
        if (var8.size() > 512) {
            var9 = new LinkedList();
        } else {
            var9 = new ArrayList();
        }

        Iterator var10 = var8.iterator();

        while (var10.hasNext()) {
            EMAMessage var11 = (EMAMessage) var10.next();
            if (var11 != null) {
                ((List) var9).add(new EMMessage(var11));
            }
        }

        return (List) var9;
    }

    public List<EMMessage> searchMsgFromDB(String var1, long var2, int var4, String var5, IMConversation.EMSearchDirection var6) {
        EMASearchDirection var7 = var6 == IMConversation.EMSearchDirection.UP ? EMASearchDirection.UP : EMASearchDirection.DOWN;
        List var8 = ((IMAConversation) this.emaObject).searchMessages(var1, var2, var4, var5, var7);
        Object var9;
        if (var8.size() > 512) {
            var9 = new LinkedList();
        } else {
            var9 = new ArrayList();
        }

        Iterator var10 = var8.iterator();

        while (var10.hasNext()) {
            EMAMessage var11 = (EMAMessage) var10.next();
            if (var11 != null) {
                ((List) var9).add(new EMMessage(var11));
            }
        }

        return (List) var9;
    }

    public List<EMMessage> searchMsgFromDB(long var1, long var3, int var5) {
        List var6 = ((IMAConversation) this.emaObject).searchMessages(var1, var3, var5);
        Object var7;
        if (var6.size() > 512) {
            var7 = new LinkedList();
        } else {
            var7 = new ArrayList();
        }

        Iterator var8 = var6.iterator();

        while (var8.hasNext()) {
            EMAMessage var9 = (EMAMessage) var8.next();
            if (var9 != null) {
                ((List) var7).add(new EMMessage(var9));
            }
        }

        return (List) var7;
    }

    public EMMessage getMessage(String var1, boolean var2) {
        EMMessage var3 = this.getCache().getMessage(var1);
        if (var3 == null) {
            EMAMessage var4 = ((IMAConversation) this.emaObject).loadMessage(var1);
            if (var4 == null) {
                return null;
            }

            var3 = new EMMessage(var4);
        }

        ((IMAConversation) this.emaObject).markMessageAsRead(var1, var2);
        return var3;
    }

    @Deprecated
    public List<EMMessage> loadMessages(List<String> var1) {
        ArrayList var2 = new ArrayList();
        Iterator var3 = var1.iterator();

        while (var3.hasNext()) {
            String var4 = (String) var3.next();
            EMAMessage var5 = ((IMAConversation) this.emaObject).loadMessage(var4);
            if (var5 != null) {
                var2.add(new EMMessage(var5));
            }
        }

        this.getCache().addMessages(var2);
        return var2;
    }

    public void markMessageAsRead(String var1) {
        ((IMAConversation) this.emaObject).markMessageAsRead(var1, true);
    }

    public List<EMMessage> getAllMessages() {
        if (this.getCache().isEmpty()) {
            EMAMessage var1 = ((IMAConversation) this.emaObject).latestMessage();
            ArrayList var2 = new ArrayList();
            if (var1 != null) {
                var2.add(new EMMessage(var1));
            }

            this.getCache().addMessages(var2);
        }

        return this.getCache().getAllMessages();
    }

    public void removeMessage(String var1) {
        EMLog.d("conversation", "remove msg from conversation: " + var1);
        ((IMAConversation) this.emaObject)._removeMessage(var1);
        this.getCache().removeMessage(var1);
    }

    public EMMessage getLastMessage() {
        if (this.getCache().isEmpty()) {
            EMAMessage var1 = ((IMAConversation) this.emaObject).latestMessage();
            EMMessage var2 = var1 == null ? null : new EMMessage(var1);
            this.getCache().addMessage(var2);
            return var2;
        } else {
            return this.getCache().getLastMessage();
        }
    }

    public EMMessage getLatestMessageFromOthers() {
        EMAMessage var1 = ((IMAConversation) this.emaObject).latestMessageFromOthers();
        EMMessage var2 = var1 == null ? null : new EMMessage(var1);
        this.getCache().addMessage(var2);
        return var2;
    }

    public void clear() {
        this.getCache().clear();
    }

    public void clearAllMessages() {
        ((IMAConversation) this.emaObject).clearAllMessages();
        this.getCache().clear();
    }

    public void setExtField(String var1) {
        ((IMAConversation) this.emaObject)._setExtField(var1);
    }

    public String getExtField() {
        return ((IMAConversation) this.emaObject).extField();
    }

    public static IMConversation.IMConversationType msgType2ConversationType(String var0, ChatType var1) {
        return var1 == ChatType.Chat ? IMConversation.IMConversationType.Chat : (var1 == ChatType.GroupChat ? IMConversation.IMConversationType.GroupChat : (var1 == ChatType.ChatRoom ? IMConversation.IMConversationType.ChatRoom : IMConversation.IMConversationType.Chat));
    }

    public boolean isGroup() {
        IMConversation.IMConversationType var1 = this.getType();
        return var1 == IMConversation.IMConversationType.GroupChat || var1 == IMConversation.IMConversationType.ChatRoom;
    }

    public void insertMessage(EMMessage var1) {
        ((IMAConversation) this.emaObject).insertMessage((EMAMessage) var1.emaObject);
        this.getCache().addMessage(var1);
    }

    public void appendMessage(EMMessage var1) {
        ((IMAConversation) this.emaObject).appendMessage((EMAMessage) var1.emaObject);
        this.getCache().addMessage(var1);
    }

    public boolean updateMessage(EMMessage var1) {
        return ((IMAConversation) this.emaObject).updateMessage((EMAMessage) var1.emaObject);
    }

    public String getMessageAttachmentPath() {
        String var1 = EMClient.getInstance().getChatConfigPrivate().D();
        return var1 + "/" + EMClient.getInstance().getCurrentUser() + "/" + this.conversationId();
    }

    IMConversation.MessageCache getCache() {
        synchronized (EMClient.getInstance().chatManager().caches) {
            IMConversation.MessageCache var1 = (IMConversation.MessageCache) EMClient.getInstance().chatManager().caches.get(((IMAConversation) this.emaObject).conversationId());
            if (var1 == null) {
                var1 = new IMConversation.MessageCache();
            }

            EMClient.getInstance().chatManager().caches.put(((IMAConversation) this.emaObject).conversationId(), var1);
            return var1;
        }
    }

    static class MessageCache {
        TreeMap<Long, EMMessage> sortedMessages = new TreeMap(new IMConversation.MessageCache.MessageComparator());
        Map<String, EMMessage> messages = new HashMap();
        Map<String, Long> idTimeMap = new HashMap();
        final boolean sortByServerTime = EMClient.getInstance().getChatConfigPrivate().b().isSortMessageByServerTime();

        MessageCache() {
        }

        public synchronized EMMessage getMessage(String var1) {
            return var1 != null && !var1.isEmpty() ? (EMMessage) this.messages.get(var1) : null;
        }

        public synchronized void addMessages(List<EMMessage> var1) {
            Iterator var2 = var1.iterator();

            while (var2.hasNext()) {
                EMMessage var3 = (EMMessage) var2.next();
                this.addMessage(var3);
            }

        }

        public synchronized void addMessage(EMMessage var1) {
            if (var1 != null && var1.emaObject != null && var1.getMsgTime() != 0L && var1.getMsgTime() != -1L && var1.getMsgId() != null && !var1.getMsgId().isEmpty() && var1.getType() != Type.CMD) {
                String var2 = var1.getMsgId();
                long var3 = this.sortByServerTime ? var1.getMsgTime() : var1.localTime();
                this.sortedMessages.put(Long.valueOf(var3), var1);
                this.messages.put(var2, var1);
                this.idTimeMap.put(var2, Long.valueOf(var3));
            }
        }

        public synchronized void removeMessage(String var1) {
            if (var1 != null && !var1.isEmpty()) {
                EMMessage var2 = (EMMessage) this.messages.get(var1);
                if (var2 != null) {
                    Long var3 = (Long) this.idTimeMap.get(var1);
                    if (var3 != null) {
                        this.sortedMessages.remove(var3);
                        this.idTimeMap.remove(var1);
                    }

                    this.messages.remove(var1);
                }

            }
        }

        public synchronized List<EMMessage> getAllMessages() {
            ArrayList var1 = new ArrayList();
            var1.addAll(this.sortedMessages.values());
            return var1;
        }

        public synchronized EMMessage getLastMessage() {
            return this.sortedMessages.isEmpty() ? null : (EMMessage) this.sortedMessages.lastEntry().getValue();
        }

        public synchronized void clear() {
            this.sortedMessages.clear();
            this.messages.clear();
            this.idTimeMap.clear();
        }

        public synchronized boolean isEmpty() {
            return this.sortedMessages.isEmpty();
        }

        class MessageComparator implements Comparator<Long> {
            MessageComparator() {
            }

            public int compare(Long var1, Long var2) {
                long var3 = var1.longValue() - var2.longValue();
                return var3 > 0L ? 1 : (var3 == 0L ? 0 : -1);
            }
        }
    }

    public static enum EMSearchDirection {
        UP,
        DOWN;

        private EMSearchDirection() {
        }
    }

    public static enum IMConversationType {
        Chat,
        GroupChat,
        ChatRoom,
        DiscussionGroup,
        HelpDesk;

        private IMConversationType() {
        }
    }
}